var _rowIndex;

//初始化页面
efform_onload = function ()
{ 
	$("#ef_region_detail").hide();
};

//点击查询按钮
button_query_onclick = function () 
{ 
	_rowIndex=null;
	clearRegion("ef_grid_d");
	$("#ef_region_detail").hide();
	efgrid.submitInqu( "ef_grid_r", "ZM", "ZMCP12", "query");
}
//复制
button_copy_onclick = function () 
{
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	if(rows.length>1){
		EFAlert("只能选择一条信息！","警告"); return;
	}
	EFConfirm('你确认要复制吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_r", "ZM","ZMCP12","copy",true);
	});

}
//点击主表新增按钮
button_insert_onclick = function (){
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		var now = new Date();
		var year = grid.getCellValueByColumnName(rows[i],"fdPlanYear");
		var fdPlanMonth = grid.getCellValueByColumnName(rows[i],"fdPlanMonth");
		grid.setCellValue(rows[i],2,year+"年"+fdPlanMonth+"月的掘进计划",TYPE_DATA);
	}
	
	efgrid.submitForm( "ef_grid_r", "ZM", "ZMCP12", "insert", true);
} 

//点击主表修改按钮
button_update_onclick = function (){ 
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")!="草拟")
		{
			EFAlert("只能对草拟的记录进行修改！","警告"); return;
		}
		var now = new Date();
		var year = grid.getCellValueByColumnName(rows[i],"fdPlanYear");
		var fdPlanMonth = grid.getCellValueByColumnName(rows[i],"fdPlanMonth");
		grid.setCellValue(rows[i],2,year+"年"+fdPlanMonth+"月的掘进计划",TYPE_DATA);
	}
	
	efgrid.submitForm( "ef_grid_r", "ZM", "ZMCP12", "update", true);
}

//点击主表删除按钮
button_delete_onclick = function (){
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")!="草拟")
		{
			EFAlert("只能对草拟的记录进行删除！","警告"); return;
		}
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_r", "ZM", "ZMCP12", "delete", true);
	});
}

//点击主表上报按钮
button_report_onclick = function (){  
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	if (rows.length > 1) {
		EFAlert("一次只能上报一条记录！","警告"); return;
	}
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")!="草拟")
		{
			EFAlert("只能上报草拟状态的掘进计划！","警告"); return;
		}
	}
	EFConfirm('你确认要上报吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_r", "ZM", "ZMCP12", "report", true);
	});
}

//点击主表撤销按钮
button_goback_onclick = function (){  
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	if (rows.length > 1) {
		EFAlert("一次只能撤回一条记录！","警告"); return;
	}
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")!="上报")
		{
			EFAlert("只能撤回上报状态的掘进计划！","警告"); return;
		}
	}
	EFConfirm('你确认要撤回吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_r", "ZM", "ZMCP12", "goback", true);
	});
}

//记录选择
efgrid_onRowClicked = function( grid_id, row_index ){
	if(grid_id == "ef_grid_r"){
		_rowIndex = row_index;
		var grid = efgrid.getGridObject(grid_id);
		var fdProfessionalplanId = grid.getCellValueByColumnName(row_index, "fdKeyId");
		var fdPlanStatus = grid.getCellValueByColumnName(row_index,"fdPlanStatus");
		
		var fdPlanYear = grid.getCellValueByColumnName(row_index,"fdPlanYear");		//从表需要的参数
		var fdPlanMonth = grid.getCellValueByColumnName(row_index,"fdPlanMonth");	//从表需要的参数
		
		//隐藏/显示从表按钮
		if(fdPlanStatus != '草拟')
			hideAndShowSubButtons(false);
		else
			hideAndShowSubButtons(true);
		
		var info = new EiInfo();
		info.set("fdProfessionalplanId", fdProfessionalplanId);
		
		$('#fdProfessionalplanId').val(fdProfessionalplanId);
		$('#fdPlanYear').val(fdPlanYear);
		$('#fdPlanMonth').val(fdPlanMonth);
		
		EiCommunicator.send("ZMCP13", "initLoad", info, ajax_callback_getdetail);
	}
}

//主表选择成功后的回调
//刷新冲表
var ajax_callback_getdetail= {
	onSuccess: 
		function(eiInfo) { 
			efwindow.hide();
			var grid = efgrid.getGridObject("ef_grid_d");
			grid.refresh(eiInfo);
			$('#ef_region_detail').show();
		},
	onFail:
		function(eMsg)
		{
			EFAlert("明细记录查询失败，原因：" + eMsg ,"警告"); 
		}
}

/****************************从表操作******************************/

//从表新增按钮
button_insertdetail_onclick = function (){
	if (isSelected("ef_grid_d") == 0)return;
	efgrid.submitForm("ef_grid_d", "ZM", "ZMCP13", "insert", true);
}

//从表修改按钮
button_updatedetail_onclick = function (){
	if (isSelected("ef_grid_d") == 0)return;
	efgrid.submitForm("ef_grid_d", "ZM", "ZMCP13", "update", true);
}

//从表删除按钮
button_deletedetail_onclick = function (){
	var grid = isSelected("ef_grid_d");
	if (grid == 0) return;
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if(r){
			efgrid.submitForm("ef_grid_d", "ZM", "ZMCP13", "delete", true);
		}
	}); 
} 

//新增、修改、删除后的执行方法
efgrid_onAjaxSubmitSuccess = function(gridId, service_name, method_name, eiInfo) {
	if(gridId == "ef_grid_d") {
		if (eiInfo.status != -1) {
			var grid = efgrid.getGridObject("ef_grid_d");
			grid.refresh( eiInfo );
			if(method_name != "query")
			{
				refresh_main_grid();
			}
		}
		else
		{
			submitCallBackEiMsg = eiInfo.msg;
		}
	} else {
		var grid = efgrid.getGridObject(gridId);
		grid.refresh( eiInfo );
		clearRegion("ef_grid_d");
		$("#ef_region_detail").hide();
	}
}

//点击了新增行之后，给一些默认数据
//主表新增时，给默认数据
efgrid_afterAddNewRow = function(grid_id){ 
	var grid = efgrid.getGridObject(grid_id);
	var rows = grid.getCheckedRows();
	if(grid_id=="ef_grid_r")
	{
	    var now = new Date();
	    var year = now.getFullYear();       //年
	    var month = now.getMonth() + 1;     //月
	    if(month > 12) {
	    	year = year + 1;				//下一年
	    	month = 1;						//第一个月
	    }
	    if(month < 10) {
	    	month = "0" + month;
	    }
	    var day = now.getDate();            //日 
	    var clock = year + "-" + month + "-";
	    if(day < 10) clock += "0";
	    clock += day + " "; 
	    
		for(var i = 0 ; i < rows.length;i++){
			grid.getBlockData().setCell(rows[i], "fdPlanStatus", "草拟");
			grid.getBlockData().setCell(rows[i], "fdPlanYear", year);
			grid.getBlockData().setCell(rows[i], "fdPlanMonth", month);
			grid.getBlockData().setCell(rows[i], "fdPlanType", "掘进计划");
			grid.getBlockData().setCell(rows[i], "fdTitle", year+"年"+month+"月的掘进计划");
			grid.getBlockData().setCell(rows[i], "fdJcSum", 0.00);
			grid.getBlockData().setCell(rows[i], "fdUnit", "m");
			grid.getBlockData().setCell(rows[i], "fdCreateDate", clock);
		}
	}
	for(var i = 0 ; i < rows.length;i++){
		grid.getBlockData().setCell(rows[i], "fdKeyId", "");
	}
}

//从表单元格编辑后回调的方法
efgrid_onDataCellSaved = function( grid_id, row_index, col_index, cell_value ) 
{
	  var grid = efgrid.getGridObject( grid_id ); 
	  if(grid_id == 'ef_grid_d' && col_index == 1) 
	  { 
		  var info=_getEi();
		  var eiBlock=info.getBlock("cmes.cp.roadway");
		  for(var i=0;i<eiBlock.getRows().length;i++)
		  {
 			  var _value = eiBlock.getCell(i,"fdKeyId");
			  if(_value==cell_value)
			  {
				  var fdBoringProcess = eiBlock.getCell(i,"fdBoringProcess");		//掘进方式
				  var fdRoadwayType = eiBlock.getCell(i,"fdRoadwayType");			//工程类别
				  var fdMineRockId = eiBlock.getCell(i,"fdMineRockId");				//煤岩类别
				  var fdSupportingTypeId = eiBlock.getCell(i,"fdSupportingTypeId");//支护方式
				  var fdRoadwayUsing = eiBlock.getCell(i,"fdRoadwayUsing");			//巷道用途
				  
				  grid.setCellValue(row_index, 3, fdBoringProcess, TYPE_DATA);
				  grid.setCellValue(row_index, 5, fdRoadwayType, TYPE_DATA);
				  grid.setCellValue(row_index, 6, fdMineRockId, TYPE_DATA);
				  grid.setCellValue(row_index, 7, fdSupportingTypeId, TYPE_DATA);
				  grid.setCellValue(row_index, 8, fdRoadwayUsing, TYPE_DATA);

				  grid.refreshCell( row_index, 3 , TYPE_DATA );
				  grid.refreshCell( row_index, 5 , TYPE_DATA );
				  grid.refreshCell( row_index, 6 , TYPE_DATA );
				  grid.refreshCell( row_index, 7 , TYPE_DATA );
				  grid.refreshCell( row_index, 8 , TYPE_DATA );
			  }
		  }
	}
}

/*****************公用方法区******************/
//根据主表选中记录的状态，确定从表的按钮是否显示
var hideAndShowSubButtons=function(status)
{
	if(!status)
	{
		efbutton.setButtonStatus("insertdetail", false);
		efbutton.setButtonStatus("updatedetail", false);
		efbutton.setButtonStatus("deletedetail", false);
	}else
	{
		efbutton.setButtonStatus("insertdetail", true);
		efbutton.setButtonStatus("updatedetail", true);
		efbutton.setButtonStatus("deletedetail", true);
	}
}

//刷新主表方法
refresh_main_grid = function() {
	var ajax_callback =
	{
		onSuccess :
    		function(eiInfo)
			{
				grid = efgrid.getGridObject("ef_grid_r");
				grid.refresh(eiInfo);
				grid.setCurrentRowIndex(_rowIndex);
    		},
  		onFail:
    		function(eMsg)
			{
  				EFAlert("服务调用失败，原因：" + eMsg ,"警告");
			}
	};

	var info = new EiInfo();
  	EiCommunicator.send( "ZMCP12", "initLoad", info, ajax_callback );
}

